package DAO;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class NotesDAO {
	public static final String KEY_TITLE = "title";
    public static final String KEY_DATE = "date";
    public static final String KEY_BODY = "body";
    public static final String KEY_ROWID = "_id";
    public static final String KEY_URLFOLDER = "_folder";
    
    private static DatabaseHelper mDbHelper;
    private static SQLiteDatabase mDb;
    
    private static final String DATABASE_TABLE = "notes";

    public static void open(Context ctx) throws SQLException {
        mDbHelper = new DatabaseHelper(ctx);
        mDb = mDbHelper.getWritableDatabase();
    }

    public void close() {
        mDbHelper.close();
    }

    public static long createNote(String title, String date, 
    		String body,String folder) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_DATE, date);
        initialValues.put(KEY_BODY, body);
        initialValues.put(KEY_URLFOLDER, folder);
        Log.i("Notes","Them 1 note thanh cong");
        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }
    
    public static boolean deleteNote(long rowId) {
        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    public static Cursor fetchAllNotes() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_DATE,
                KEY_BODY,KEY_URLFOLDER}, null, null, null, null, KEY_DATE+" desc");
    }

    public static Cursor fetchNote(long rowId) throws SQLException {

        Cursor mCursor =
            mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                    KEY_TITLE,KEY_DATE, KEY_BODY,KEY_URLFOLDER}, KEY_ROWID + "=" + rowId, null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }
    public static Cursor getNote(String name)
    {
    	//NotesDTO temp = new NotesDTO();
    	Cursor mCursor =
                mDb.rawQuery("select "+ KEY_ROWID + "," + KEY_TITLE + "," 
                		+ KEY_DATE + "," + KEY_BODY + "," + KEY_URLFOLDER +
                		" from notes where "+ KEY_TITLE + "='" + name + "'",
                		null);
    	/*
    	temp.setMaGhiChu(mCursor.getLong(mCursor.getColumnIndexOrThrow(KEY_ROWID)));
    	temp.setTieuDe(mCursor.getString(mCursor.getColumnIndexOrThrow(KEY_TITLE)));
    	temp.setNoiDung(mCursor.getString(mCursor.getColumnIndexOrThrow(KEY_BODY)));
    	temp.setThoigian(mCursor.getString(mCursor.getColumnIndexOrThrow(KEY_DATE)));
    	*/
    	

    	
    	return mCursor;
    }
    public static boolean updateNote(long rowId, String title, String date,
    		String body,String urlFolder) {
        ContentValues args = new ContentValues();
        args.put(KEY_TITLE, title);
        args.put(KEY_DATE, date);
        args.put(KEY_BODY, body);
        args.put(KEY_URLFOLDER, urlFolder);
        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}
